package com.vf.admin.client.service.impl;


import com.vf.admin.client.dao.*;
import com.vf.admin.client.service.AppFamilyMemberService;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.utils.AgeUtils;
import com.vf.admin.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.vf.admin.common.Constants.guizuCacheMap;

@Service
public class AppFamilyMemberServiceImpl implements AppFamilyMemberService {

    @Autowired
    private AppFamilyMemberDao appFamilyMemberDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private AppFamilyPrestigeDao appFamilyPrestigeDao;
    @Autowired
    private AppFamilyIdentityDao appFamilyIdentityDao;
    @Autowired
    private AppFamilyDao appFamilyDao;

    @Override
    public void save(AppFamilyMember appFamilyMember) {
        appFamilyMemberDao.add(appFamilyMember);
    }

    @Override
    public Boolean addFamilyMember(AppFamilyMember appFamilyMember) {
        /*AppFamilyMember info = appFamilyMemberDao.findInfo(appFamilyMember);
        if(info!=null && info.getStatus()==2){
            info.setIdentity(4);
            info.setCreatetime(new);
        }*/
        appFamilyMemberDao.add(appFamilyMember);
        return true;
    }

    @Override
    public AppFamilyMember findInfo(AppFamilyMember appFamilyMember) {
        return appFamilyMemberDao.findInfo(appFamilyMember);
    }

    @Override
    public AppFamilyMember findByUserId(Integer userid) {
        return appFamilyMemberDao.findByUserId(userid);
    }

    @Override
    public AppFamilyMember findById(Integer id) {
        return appFamilyMemberDao.findById(id);
    }

    @Override
    public void familyMemberExamine(Integer id, Integer type) {
        AppFamilyMember appFamilyMember = new AppFamilyMember();
        appFamilyMember.setStatus(type);
        appFamilyMember.setUpdatetime(new Date());
        appFamilyMember.setId(id);
        appFamilyMemberDao.update(appFamilyMember);
    }

    @Override
    public void quitFamily(Integer id) {
        AppFamilyMember appFamilyMember = new AppFamilyMember();
        appFamilyMember.setStatus(2);
        appFamilyMember.setUpdatetime(new Date());
        appFamilyMember.setId(id);
        appFamilyMemberDao.update(appFamilyMember);
    }

    @Override
    public AppFamilyMember getFamily(Integer userid) {
        return appFamilyMemberDao.getFamily(userid);
    }

    @Override
    public List<AppFamilyMember> getFamilyMemberByFid(Integer fid) {
        List<AppFamilyMember> familyMemberByFid = appFamilyMemberDao.getFamilyMemberByFid(fid);
        if(familyMemberByFid!=null && familyMemberByFid.size()>0){
            familyMemberByFid.forEach(e->{
                AppUser appUser = appUserDao.get(e.getUserid());
                if(appUser != null) {
                    e.setPic(appUser.getPic());
                    e.setNick(appUser.getNick());
                    e.setSex(appUser.getSex());
                    e.setYeraOld(AgeUtils.getAgeByBirth(appUser.getBirthday()));
                    AppFamilyPrestige dayPrestigenum = appFamilyPrestigeDao.getDayPrestigenum(e.getUserid());
                    if(dayPrestigenum.getDayPrestigenum()==null || dayPrestigenum.getDayPrestigenum().compareTo(BigDecimal.ZERO)<=0){
                        e.setDayPrestige(BigDecimal.ZERO);
                    }else{
                        e.setDayPrestige(dayPrestigenum.getDayPrestigenum());
                    }
                    //判断用户是否是贵族
                    if (appUser.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(appUser.getNobleid());
                        e.setMedal(medal);
                    } else {
                        e.setMedal("");
                    }
                    e.setNobleid(appUser.getNobleid());
                    e.setIsguizu(appUser.getIsguizu());
                    e.setLogincode(appUser.getTengxuncode());
                    if(appUser.getVipEndDate() != null ) {
                        e.setIsVip(TimeUtils.afterNow(appUser.getVipEndDate())? 1: 0);
                    }
                }
            });
        }
        return familyMemberByFid;
    }

    @Override
    public List<AppFamilyMember> getAllFamilyMemberByFid(Integer fid) {
        List<AppFamilyMember> familyMemberByFid = appFamilyMemberDao.getFamilyMemberByFid(fid);
        return familyMemberByFid;
    }

    @Override
    public void updateMemberIdentity(Integer id, Integer identityId) {
        appFamilyMemberDao.updateMemberIdentity(id,identityId);
    }

    @Override
    public List<AppFamilyMember> getFamilyReviewedMemberByFid(Integer fid) {
        List<AppFamilyMember> familyReviewedMemberByFid = appFamilyMemberDao.getFamilyReviewedMemberByFid(fid);
        if(familyReviewedMemberByFid!=null && familyReviewedMemberByFid.size()>0){
            familyReviewedMemberByFid.forEach(e->{
                AppUser appUser = appUserDao.get(e.getUserid());
                e.setNick(appUser.getNick());
                e.setPic(appUser.getPic());
                e.setYeraOld(AgeUtils.getAgeByBirth(appUser.getBirthday()));
                e.setSex(appUser.getSex());
                //判断用户是否是贵族
                if (appUser.getIsguizu() == 1) {
                    //是贵族
                    //根据贵族id  获取贵族图标
                    String medal = guizuCacheMap.get(appUser.getNobleid());
                    e.setMedal(medal);
                } else {
                    e.setMedal("");
                }
                e.setNobleid(appUser.getNobleid());
                e.setIsguizu(appUser.getIsguizu());
                e.setLogincode(appUser.getTengxuncode());
            });
            return familyReviewedMemberByFid;
        }
        return null;
    }

    @Override
    public List<AppFamilyMember> afterFamilyMemberList(SplitPageBean splitPageBean, AppFamilyMember appFamilyMember) {
        List<AppFamilyMember> appFamilyMembers = appFamilyMemberDao.afterFamilyMemberList(splitPageBean, appFamilyMember);
        if(appFamilyMembers!=null && appFamilyMembers.size()>0){
            appFamilyMembers.forEach(e->{
                AppUser appUser = appUserDao.get(e.getUserid());
                if(appUser != null) {
                    e.setNick(appUser.getNick());
                    e.setPic(appUser.getPic());
                    e.setSex(appUser.getSex());
                    //判断用户是否是贵族
                    if (appUser.getIsguizu() == 1) {
                        //是贵族
                        //根据贵族id  获取贵族图标
                        String medal = guizuCacheMap.get(appUser.getNobleid());
                        e.setMedal(medal);
                    } else {
                        e.setMedal("");
                    }
                    e.setNobleid(appUser.getNobleid());
                    e.setIsguizu(appUser.getIsguizu());
                    e.setLogincode(appUser.getTengxuncode());
                    e.setLoginName(appUser.getLoginname());
                    AppFamilyIdentity identity = appFamilyIdentityDao.getIdentity(e.getIdentity());
                    e.setIdentityName(identity.getName());
                    AppFamily byId = appFamilyDao.findByIdAll(e.getFid());
                    e.setFamilyName(byId.getFamilyname());
                }
            });
        }
        return appFamilyMembers;
    }
}
